package cn.picclife.mwx.app.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;


public class PropertiesUtils {
	private static Log log = LogFactory.getLog(PropertiesUtils.class);

	private static Properties prop;
	
	static {
		 prop = new Properties();
		 String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		InputStream is=null;
		 try {
			 is = new FileInputStream(path + "/upload.properties");
			prop.load(is);
		} catch (Exception e) {
			log.error(e);
		}finally {
		 	try{
		 		if(is !=null){
				is.close();
			}}catch (Exception e){
		 		log.error(e);
			}
		 }
	}
	
	 //根据Key读取Value
     public static String GetValueByKey(String key) {
         return prop.getProperty(key);
     }
     
     public static void deleteByKey(String key) {
         prop.remove(key);
         save();
     }
     
     public static void setValue(String key,String value) {
    	 prop.setProperty(key, value);
    	 save();
     }
     
     private static  void save() {
    	 String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
    	 OutputStream os = null;
		try {
			os = new FileOutputStream(path+"/upload.properties");
			prop.store(os, null);
		} catch (Exception e) {
			log.error(e);
		}finally {
			try {
				os.close();
			} catch (IOException e) {
				log.error(e);
			}
		}
     }
     public static void main(String[] args) {
		String a=PropertiesUtils.GetValueByKey("url");
		//System.out.println(a);
	}
     
}
